#include "e2d/core/render/CameraViewEdit.h"
#include "e2d/core/trans/TransformNode.h"
#include "e2d/core/Entity.h"

void e2d::CameraViewEdit::OnLoad()
{
	e2d::CameraView::OnLoad();
	int m_Width = m_ProjData.m_OrthographicWidth;
	int m_Height = m_ProjData.m_OrthographicHeight;


	{
		fxcc::graph::common::RenderTargetJack::Desc desc(1, m_Width, m_Height, m_NrComponent, false);
		m_PickRT = e2d::GraphImpl::CreateRT(desc);
	}
	{
		const int NumGBufferTextures = 3;
		const int NrComponentGBuffer = 3;

		fxcc::graph::common::RenderTargetJack::Desc desc(NumGBufferTextures, m_Width, m_Height, NrComponentGBuffer, true);
		m_GBufferRT = e2d::GraphImpl::CreateRT(desc);
	}
	{
		m_ParentEntity.LoadFromScene(m_Entity->m_Scene);
		for (auto& it : m_EntityKeyMaps)
		{
			it.m_EntityMap.LoadFromScene(m_Entity->m_Scene);
		}
	}
}

void e2d::CameraViewEdit::OnStart()
{
}
;

void e2d::CameraViewEdit::OnUpdate()
{

	if (m_FreeMove)
	{
		auto& transform = m_Entity->Get<e2d::TransformNode>();
		transform.m_Local.FreeMove(*Input(), Timer()->DeltaTime());
	}
}
;

e2d::CameraViewEdit::CameraViewEdit()
{

}


void e2d::CameraViewEdit::ClearPickupMap()
{
	m_PickupIndex = 0;
	m_PickupEntityMap.clear();
}

e2d::Entity* e2d::CameraViewEdit::GetEntityByPickupIndex(int selectPickupIndex)
{
	//ztclog::info("select index %d", selectPickupIndex);

	auto it = m_PickupEntityMap.find(selectPickupIndex);
	if (it != m_PickupEntityMap.end())
	{
		auto entity = it->second;
		return entity;
	}
	return 0;
}

void e2d::CameraViewEdit::BeginPickRTT() 
{
	ClearPickupMap();
	m_PickRT->Begin();
	m_PickRT->ClearColor({ 0,0,0,1 });
	m_PickRT->ClearBuffer(true, true);
}

void e2d::CameraViewEdit::EndPickRTT() const
{
	m_PickRT->End();
}

void e2d::CameraViewEdit::BeginGBufferRTT()
{
	m_GBufferRT->Begin();
	m_GBufferRT->ClearColor({ 0,0,0,1 });
	m_GBufferRT->ClearBuffer(true, true);
}

void e2d::CameraViewEdit::EndGBufferRTT()
{
	m_GBufferRT->End();
}




void e2d::CameraViewEdit::RegisterPickup(int id, e2d::Entity* entity)
{
	m_PickupEntityMap[id] = entity;
}


